package cn.lingyangwl.agile.msg.push.controller;

import cn.lingyangwl.agile.common.security.utils.SecurityUtils;
import cn.lingyangwl.agile.infra.InfraApi;
import cn.lingyangwl.agile.infra.common.QrCodeSceneEnum;
import cn.lingyangwl.agile.infra.common.ScanScene;
import cn.lingyangwl.agile.model.module.auth.LoginUser;
import cn.lingyangwl.agile.msg.push.model.rqrs.receiver.*;
import cn.lingyangwl.agile.msg.push.model.rqrs.notice.SubscribeMsgNoticeResp;
import cn.lingyangwl.agile.msg.push.service.ReceiverService;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.framework.core.response.Resp;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 消息接收者
 * 
 * @author shenguangyang
 */
@Api(tags = "消息接收者")
@RestController
@RequestMapping("/msgReceiver")
public class ReceiverController {
    @Resource
    private ReceiverService receiverService;
    @Resource
    private InfraApi infraApi;

    @ApiOperation(value = "分页消息接收者")
    @GetMapping("/page")
    @RamResource(action = "msg:receiver:query", name = "查询消息接收人", type = ActionType.READ)
    public Resp<IPage<ReceiverResp>> pageReceiver(@Validated ReceiverPageReq req) {
        return Resp.ok(receiverService.pageReceiver(req));
    }

    @ApiOperation(value = "通过id查找消息接收者")
    @GetMapping("/get")
    @RamResource(action = "msg:receiver:query", name = "查询消息接收人", type = ActionType.READ)
    public Resp<ReceiverResp> getReceiver(@RequestParam(value = "id") Long id) {
        return Resp.ok(receiverService.getReceiver(id));
    }
  
    @ApiOperation(value = "保存消息接收者")
    @PostMapping("/save")
    @RamResource(action = "msg:receiver:save", name = "保存消息接收人", type = ActionType.WRITE)
    public Resp<?> saveReceiver(@Validated @RequestBody ReceiverSaveReq req) {
        return Resp.ok(receiverService.saveReceiver(req));
    }
  
    @ApiOperation(value = "更新消息接收者")
    @PostMapping("/update")
    @RamResource(action = "msg:receiver:update", name = "更新消息接收人", type = ActionType.WRITE)
    public Resp<?> updateReceiver(@Validated @RequestBody ReceiverUpdateReq req) {
        return Resp.ok(receiverService.updateReceiver(req));
    }

    @ApiOperation(value = "获取任务订阅者")
    @GetMapping("/listMsgSubscriber")
    @RamResource(action = "msg:subscriber:query", name = "查询消息通知接收人", type = ActionType.READ)
    public Resp<List<ReceiverResp>> listMsgSubscriber(@RequestParam("noticeId") Long noticeId) {
        return Resp.ok(receiverService.listMsgSubscriber(noticeId));
    }

    @ApiOperation(value = "保存消息通知订阅者")
    @PostMapping("/saveMsgSubscriber")
    @RamResource(action = "msg:subscriber:save", name = "添加消息通知接收人", type = ActionType.WRITE)
    public Resp<?> saveMsgSubscriber(@Validated @RequestBody MsgSubscriberChangeReq req) {
        receiverService.saveMsgSubscriber(req);
        return Resp.ok();
    }

    @ApiOperation(value = "查询接收人订阅的消息通知")
    @GetMapping("/listSubscribeMsg")
    @RamResource(action = "msg:receiverSubscribeMsg:query", name = "查询接收人订阅的消息通知", type = ActionType.READ)
    public Resp<List<SubscribeMsgNoticeResp>> listSubscribeMsg(@Validated SubscribeMsgListReq req) {
        return Resp.ok(receiverService.listSubscribeMsg(req));
    }

    @ApiOperation(value = "删除任务订阅者")
    @DeleteMapping("/deleteMsgSubscriber")
    @RamResource(action = "msg:msgReceiver:delete", name = "删除任务接收人", type = ActionType.WRITE)
    public Resp<?> deleteMsgSubscriber(@Validated MsgSubscriberChangeReq req) {
        receiverService.deleteMsgSubscriber(req);
        return Resp.ok();
    }

    /**
     * 给接收人发送验证码
     */
    @ApiOperation(value = "发送验证消息")
    @PostMapping("/sendCaptcha")
    @RamResource(action = "msg:verifyReceiver:sendCaptcha", name = "发送验证接收人验证码", type = ActionType.WRITE)
    public Resp<?> sendCaptcha(@Validated @RequestBody SendCaptchaToReceiverReq req) {
        receiverService.sendCaptcha(req);
        return Resp.ok();
    }

    /**
     * 来自管理端的验证邮箱/手机号, 需要对方提供手机号或者邮箱验证码
     */
    @ApiOperation(value = "验证邮箱/手机号")
    @PostMapping("/checkCaptchaFromAdmin")
    @RamResource(action = "msg:verifyReceiver:checkCaptcha", name = "校验验证接收人的验证码", type = ActionType.WRITE)
    public Resp<?> checkCaptchaFromAdmin(@Validated @RequestBody ReceiverVerifyReq req) {
        receiverService.checkCaptchaFromAdmin(req);
        return Resp.ok();
    }
  
    @ApiOperation(value = "批量删除消息接收者")
    @DeleteMapping("/batchDelete")
    @RamResource(action = "msg:receiver:delete", name = "删除消息接收人", type = ActionType.WRITE)
    public Resp<?> batchDeleteReceiver(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(receiverService.batchDeleteReceiver(ids));
    }
  
    @ApiOperation(value = "删除消息接收者")
    @DeleteMapping("/delete")
    @RamResource(action = "msg:receiver:delete", name = "删除消息接收人", type = ActionType.WRITE)
    public Resp<?> deleteReceiver(@RequestParam(value = "id") Long id) {
        return Resp.ok(receiverService.deleteReceiver(id));
    }

    @ApiOperation(value = "获取微信公众号二维码")
    @GetMapping("/getWxMpQrcode")
    @RamResource(action = "msg:receiver:getWxMpQrcode", name = "获取微信公众号二维码", type = ActionType.WRITE)
    public Resp<String> getWxMpQrcode() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        JSONObject params = new JSONObject();
        params.put(ScanScene.USER_ID, loginUser.getUserId());
        params.put(ScanScene.LOGIN_ID, loginUser.getLoginId());

        ScanScene req = ScanScene.builder().isRefresh(false)
                .params(params).sceneCode(QrCodeSceneEnum.GET_OPENID.getCode()).build();
        String mpQrcode = infraApi.getMpQrcode(req);
        return Resp.ok(mpQrcode);
    }
}
